package com.ccp.dev.system.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.system.dao.UserPositionDao;
import com.ccp.dev.system.model.Position;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.model.UserPosition;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户岗位 Service类
 * @author y
 */
@Service
public class UserPositionService extends BaseService<UserPosition> {
	@Resource
    UserPositionDao userPositionDao;
    @Resource
    PositionService positionService;
    @Resource
    UserRoleService userRoleService;
    @Resource
    UserUnderService userUnderService;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    SysOrgService sysOrgService;

    /**
     * 根据用户id或者岗位id获取数据
     * @param userId    用户id
     * @param posId     岗位id
     * @return  list
     */
    public List queryByUserIdOrPosId(String userId,String posId){
        List list = new ArrayList();
        if(StringUtil.isNotEmpty(userId)){
            list = this.queryByUserId(userId);
        }else if(StringUtil.isNotEmpty(posId)){
            Position position = this.positionService.getOneById(posId);
            list.add(position);
        }
        return list;
    }
//    /**
//     *添加岗位信息
//     * @param userId            用户
//     * @param orgId             组织
//     * @param posIds            岗位
//     * @param personCharges     主要负责人
//     * @param mainPosts         主岗位
//     */
//    @Transactional(rollbackFor = Exception.class)
//    public void  addUserPosition(String userId,String orgId,String[] posIds,String[] personCharges,String mainPosts){
//        if(null == posIds || posIds.length == 0){
//            //查一下这个人有没有关联数据
//            List<UserPosition> list = userPositionDao.queryOrgByUserId(userId);
//            if(list==null || list.isEmpty()){
//                //没有增加过增加
//                this.userPositionDao.add(addBaseUserPosition(userId,orgId));
//            }
//        }else{
//            //先清空当前用户关联的岗位信息
//            this.userPositionDao.delByUserId(userId);
//            for(String posId : posIds){
//                Position position = this.positionService.getOneById(posId);
//                UserPosition userPosition = addBaseUserPosition(userId,orgId);
//                userPosition.setJobId(position.getJobId());
//                userPosition.setPosId(posId);
//                //匹配是否是主要负责人
//               if(null != personCharges && personCharges.length > 0) {
//                   for (String charge : personCharges) {
//                       if (posId.equals(charge)) {
//                           userPosition.setIsCharge(UserPosition.CHARRGE_YES);
//                           break;
//                       }
//                   }
//               }
//                //匹配是否是主岗位
//                if(posId.equals(mainPosts)){
//                    userPosition.setIsPrimary(UserPosition.PRIMARY_YES);
//                }
//                this.userPositionDao.add(userPosition);
//            }
//        }
//    }

    /**
     *添加岗位信息
     * @param userId            用户
     * @param orgId             组织
     * @param posIds            岗位
     * @param personCharges     主要负责人
     * @param mainPosts         主岗位
     * @param primOrId         主组织id
     * @param orgchargs         组织主要负责人
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void  addUserPosition(String userId,String orgId,String[] posIds,String[] personCharges,String mainPosts,String primOrId,String orgchargs){
        List<UserPosition> list = userPositionDao.queryOrgByUserId(userId);
        if(list==null || list.isEmpty()){
            for(String orgid:orgId.split(",")){
                UserPosition userPosition = new UserPosition();
                userPosition.setUserPosId(UUIDUtils.getUUIDFor32());
                userPosition.setUserId(userId);
                userPosition.setOrgId(orgid);
                if(null!=orgchargs&&orgchargs.contains(orgid)){
                    userPosition.setIsCharge(UserPosition.CHARRGE_YES);
                }else{
                    userPosition.setIsCharge(UserPosition.CHARRGE_NO);
                }
                if(null!=primOrId&&primOrId.equals(orgid)){
                    userPosition.setIsPrimary(UserPosition.PRIMARY_YES);
                }else{
                    userPosition.setIsPrimary(UserPosition.PRIMARY_NO);
                }
                userPosition.setIsDelete(UserPosition.DELETE_NO);
                this.userPositionDao.add(userPosition);
            }
        }else{
            this.userPositionDao.delByUserId(userId);
            for(String orgid:orgId.split(",")){
                UserPosition userPosition = new UserPosition();
                userPosition.setUserPosId(UUIDUtils.getUUIDFor32());
                userPosition.setUserId(userId);
                userPosition.setOrgId(orgid);
                if(orgchargs.contains(orgid)){
                    userPosition.setIsCharge(UserPosition.CHARRGE_YES);
                }else{
                    userPosition.setIsCharge(UserPosition.CHARRGE_NO);
                }
                if(primOrId.equals(orgid)){
                    userPosition.setIsPrimary(UserPosition.PRIMARY_YES);
                }else{
                    userPosition.setIsPrimary(UserPosition.PRIMARY_NO);
                }
                userPosition.setIsDelete(UserPosition.DELETE_NO);
                this.userPositionDao.add(userPosition);
            }
        }

    }


    /**
     *公共的添加用户岗位信息
     * @param userId    用户
     * @param orgId     组织
     * @return UserPosition
     */
    @Transactional(rollbackFor = Exception.class)
    public UserPosition addBaseUserPosition(String userId,String orgId){
        UserPosition userPosition = new UserPosition();
        userPosition.setUserPosId(UUIDUtils.getUUIDFor32());
        userPosition.setUserId(userId);
        userPosition.setOrgId(orgId);
        userPosition.setIsCharge(UserPosition.CHARRGE_NO);
        userPosition.setIsPrimary(UserPosition.PRIMARY_NO);
        userPosition.setIsDelete(UserPosition.DELETE_NO);
        //如果当钱orgid为空， 给他默认为当前登录人orgid
        if(StringUtil.isEmpty(orgId)){
            userPosition.setOrgId(ContextUtil.getCurrentOrgId());
        }
        return userPosition;
    }

    /**
     * 删除某用户的岗位关联关系
     * @param userId 用户id
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByUserId(String userId){
        this.userPositionDao.delByUserId(userId);
    }

    /**
     * 更新用户组织
     * @param userId 用户id
     */
    @Transactional(rollbackFor = Exception.class)
    public void updUserOrg(String userId,String orgId,int isKeep) throws Exception{
        if(userId.equals(ContextUtil.getCurrentUserId())){
            throw new Exception("sysUser.editUser.changeUserOrgWarning");
        }
        boolean isContain = this.sysRoleService.isContainThreeRole(userId);
        if(isContain){
            throw new Exception("sysUser.editUser.threeUserWarning");
        }
        SysOrg sysOrg = sysOrgService.getOrgByUserId(userId);
        if(sysOrg.getOrgId().equals(orgId)){
            throw new Exception("sysUser.editUser.changeUserWarning");
        }
        this.userPositionDao.delByUserId(userId);
        UserPosition userPosition = addBaseUserPosition(userId,orgId);
        userPositionDao.add(userPosition);
        if(isKeep==0){
            userRoleService.delByUserId(userId);
        }
        userUnderService.delByUpOrDown(userId);
    }

    /**
     * 获取某用户的岗位关联关系
     * @param userId 用户id
     * @return List<UserPosition>
     */
    public List<UserPosition> queryByUserId(String userId){
       return this.userPositionDao.queryByUserId(userId);
    }

    /**
     * 查询是否存在非三员用户
     * @param orgId 组织id
     * @return 若不存在非三员用户返回false 存在返回true
     */
    public boolean isExistsUser(String orgId) {
        List<SysRole> roleList = userPositionDao.isExistsUser(orgId);
        return roleList.size()>0;
    }

    /**
     * 删除用户与组织关联表数据
     * @param orgId 组织id
     */
    public void delByOrgId(String orgId) {
        userPositionDao.delByOrgId(orgId);
    }

    /**
     *  根据组织id 获取组织成员 姚陆洋
     * @param orgId
     * @return
     */
    public List<UserPosition> queryUserByOrgIds(String orgId){
        return userPositionDao.queryUserByOrgIds(orgId);
    }

    public List<UserPosition> queryPrimaryOrgByUserIds(List<String> userIds) {
        if(userIds==null || userIds.size() == 0)
            return new ArrayList<>();
        return userPositionDao.queryPrimaryOrgByUserIds(userIds);
    }

    public void batchDeleteByUserId(List<String> userIds) {
        userPositionDao.batchDeleteByUserId(userIds);
    }
}
